package com.sakura.mall.ware.purchaseDetail.service.impl;

import com.sakura.mall.common.response.Page;
import com.sakura.mall.ware.purchaseDetail.domain.entity.PurchaseDetailEntity;
import com.sakura.mall.ware.purchaseDetail.domain.request.AddPurchaseDetailRequest;
import com.sakura.mall.ware.purchaseDetail.domain.request.UpdatePurchaseDetailRequest;
import com.sakura.mall.ware.purchaseDetail.domain.request.PurchaseDetailPageRequest;
import com.sakura.mall.ware.purchaseDetail.domain.vo.PurchaseDetailVo;
import com.sakura.mall.ware.purchaseDetail.mapper.PurchaseDetailMapper;
import com.sakura.mall.ware.purchaseDetail.service.PurchaseDetailService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 *  service impl
 *
 * @author sakura
 */
@Service
@RequiredArgsConstructor
public class PurchaseDetailServiceImpl extends ServiceImpl<PurchaseDetailMapper, PurchaseDetailEntity> implements PurchaseDetailService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(AddPurchaseDetailRequest request) {
        // todo 其他校验逻辑
        PurchaseDetailEntity purchaseDetail = request.convert(PurchaseDetailEntity.class);
        this.save(purchaseDetail);
    }

    @Override
    public Page<PurchaseDetailVo> queryPage(PurchaseDetailPageRequest request) {
        IPage<PurchaseDetailEntity> qp = request.toPage();
        LambdaQueryWrapper<PurchaseDetailEntity> queryWrapper = new LambdaQueryWrapper<>();
        this.page(qp, queryWrapper);
        IPage<PurchaseDetailVo> ret = qp.convert(entity -> entity.convert(PurchaseDetailVo.class));
        return Page.convert(ret);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateById(Long id, UpdatePurchaseDetailRequest request) {
        PurchaseDetailEntity entity = request.convert(PurchaseDetailEntity.class);
        entity.setId(id);
        this.updateById(entity);
    }

}

